package com.sun.showcase.controller.testcase;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.showcase.biz.testcase.service.TaskCaseService;
import com.sun.showcase.biz.testcase.service.TestTaskService;
import com.sun.showcase.client.testcase.domain.TaskCase;
import com.sun.showcase.client.testcase.domain.TestTask;
import com.sun.showcase.client.testcase.query.TaskCaseQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Json;

@Controller
@RequestMapping(value="/testcase/taskCase") 
public class TaskCaseController{
	
	@Autowired
	private TaskCaseService taskCaseService;
	@Autowired
	private TestTaskService testTaskService;
	
	private Json json = new Json();
	/**
	 * 跳转到TaskCase管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goTaskCase")
	public String goTaskCase() {
		return "testcase/taskCase";
	}
	/**
	 * 跳转到TaskCase管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goFrontendTaskCase")
	public String goFrontendTaskCase(@RequestParam(value="taskId",required=false) Long taskId ,ModelMap map) {
		TestTask task = testTaskService.get(taskId);
		map.put("taskId", task.getId());
		map.put("taskName", task.getName());
		return "/frontend/testcase/taskCase";
	}
	/**
	 * 跳转到TaskCase新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value="handleTaskCase")
	public String handleTaskCase(@RequestParam(value="id",required=false) Long id ,ModelMap map) {
		return "testcase/taskCase_rowEdit";
	}

	/**
	 * 跳转到查看desc页面
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="showDesc")
	public TaskCase showDesc(TaskCaseQuery taskCaseQuery) {
		return taskCaseService.get(taskCaseQuery);
	}

	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/datagrid")
	public DataGrid datagrid(TaskCaseQuery taskCaseQuery) {
		return taskCaseService.datagrid(taskCaseQuery);
	}
	

	/**
	 * 添加一个TaskCase
	 */
	@ResponseBody
	@RequestMapping(value="add")
	public Json add(TaskCaseQuery taskCaseQuery) {
		taskCaseService.add(taskCaseQuery);
		json.setSuccess(true);
		json.setMsg("添加成功！");
		return json;
	}
	/**
	 * 添加一个TaskCase
	 */
	@ResponseBody
	@RequestMapping(value="addBatch")
	public Json addBatch(@RequestParam(value="taskId") Long taskId,@RequestParam(value="taskGroupId") Long taskGroupId,@RequestParam(value="caseIds") Long[] caseIds) {
		taskCaseService.addBatch(taskId,taskGroupId,caseIds);
		json.setSuccess(true);
		json.setMsg("添加成功！");
		return json;
	}

	/**
	 * 编辑TaskCase
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Json edit(TaskCaseQuery taskCaseQuery) {
		taskCaseService.update(taskCaseQuery);
		json.setSuccess(true);
		json.setMsg("编辑成功！");
		return json;
	}

	/**
	 * 物理删除TaskCase
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Json delete(TaskCaseQuery taskCaseQuery) {
		taskCaseService.delete(taskCaseQuery.getIds());
		json.setSuccess(true);
		json.setMsg("删除成功！");
		return json;
	}
	
	/**
	 * 逻辑删除TaskCase
	 */
	@ResponseBody
	@RequestMapping(value="deletePt")
	public Json deletePt(TaskCaseQuery taskCaseQuery) {
		taskCaseService.deletePt(taskCaseQuery.getIds());
		json.setSuccess(true);
		json.setMsg("删除成功！");
		return json;
	}
	/**
	 */
	@ResponseBody
	@RequestMapping(value="updateStatusBatch")
	public Json updateStatusBatch(TaskCaseQuery taskCaseQuery) {
		taskCaseService.updateStatusBatch(taskCaseQuery);
		json.setSuccess(true);
		json.setMsg("更新成功！");
		return json;
	}
	/**
	 */
	@ResponseBody
	@RequestMapping(value="updateGroupId")
	public Json updateGroupId(TaskCaseQuery taskCaseQuery) {
		taskCaseService.updateGroupId(taskCaseQuery);
		json.setSuccess(true);
		json.setMsg("更新成功！");
		return json;
	}
}
